package Chapter5;//实现一个死锁
class DeadLockThread implements Runnable{
	static Object chopsticks=new Object();
	static Object knife=new Object();
	private boolean flag;
	DeadLockThread(boolean flag){
		this.flag=flag;
	}
	public void run() {
		if(flag) {
			while(true) {
				synchronized(chopsticks) {
					System.out.println(Thread.currentThread().getName()+"---if--chopsticks");
				}
				synchronized(knife) {
					System.out.println(Thread.currentThread().getName()+"---if---knife");
				}
			}
		}else {
			while(true) {
				synchronized(knife) {
					System.out.println(Thread.currentThread().getName()+"---else---knife");
				}
				synchronized(chopsticks) {
					System.out.println(Thread.currentThread().getName()+"---else--chopsticks");
				}
				
			}
		}
	}
}
public class Example14 {
	public static void main(String[] args) {
		DeadLockThread d1=new DeadLockThread(true);
		DeadLockThread d2=new DeadLockThread(false);
		new Thread(d1,"Chinese").start();
		new Thread(d2,"American").start();
	}

}
